Date.prototype.format = function(fmt) {
  var o = {
    "M+" : this.getMonth()+1,                 //月份
    "d+" : this.getDate(),                    //日
    "h+" : this.getHours(),                   //小时
    "m+" : this.getMinutes(),                 //分
    "s+" : this.getSeconds(),                 //秒
    "q+" : Math.floor((this.getMonth()+3)/3), //季度
    "S"  : this.getMilliseconds()             //毫秒
  };
  if(/(y+)/.test(fmt)) {
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
  }
  for(var k in o) {
    if(new RegExp("("+ k +")").test(fmt)){
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
    }
  }
  return fmt;
}

Date.prototype.addDays = function (days) {
  return new Date(this.setDate( this.getDate() + days));
}

Date.prototype.equals = function (date) {
  if(typeof (date) == "undefined" || date == null)
    return false;
  if(this == date)
    return true;
  if(this.format("yyyy-MM-dd") == date)
    return true;
  if(date instanceof Date){
    return (this.getTime() == date.getTime())
  }

  if(this.format("yyyy-MM-dd") == date || this.format("yyyy-MM-dd HH:mm") == date )
    return true;

    return this.toString() == date.toString();
}

export default {

  setLoginUser: function (data) {
    sessionStorage.setItem('user', JSON.stringify(data))
  },
  getLoginUser: function () {
    return JSON.parse(sessionStorage.getItem('user'))
  },
  hasValInArrayObj: function (arr, key, val) {
    for (let i = 0; i < arr.length; i++) {
      if (arr[i][key] == val)
        return i;
    }
    return -1;
  },
  
  //判断是否来自微信
	isWeiXin:function(){
	  var ua = window.navigator.userAgent.toLowerCase();
	  if(ua.match(/MicroMessenger/i) == 'micromessenger'){
	  	return true;
	  }else{
	  	return false;
	  }
	},
  
  //获取外链参数
  getUrlKey: function (name) {
        return decodeURIComponent(
            (new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null;
  },
  isPhone:function(phone) {
	    var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
	    if (!myreg.test(phone.trim())) {
	        return false;
	    } else {
	        return true;
	    }
	},
	isMoney:function(money) {
			let reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
	    if (!reg.test(money.trim())) {
	        return false;
	    } else {
	        return true;
	    }
	}
}
